import Vue from 'vue'
import App from './App.vue'
import ElementUI from 'element-ui'
import router from './router'
import VueRouter from "vue-router";
import store from "./store";
import axios from "axios";
import * as intercept from "@/utils/intercept";
// withCredentials: true, // 允许跨域
// axios.defaults.withCredentials = true; //让ajax携带cookie
Vue.prototype.$axios = axios;
import * as echarts from 'echarts'
Vue.prototype.$echarts = echarts
// Vue.prototype.$axios=intercept
//然后将我们的axios挂载到http请求上
Vue.prototype.$http=axios
//设置默认访问路径，这个路径和你后端配置文件application.yml
axios.defaults.baseURL="http://localhost:8666"
// axios.defaults.baseURL= '/api'
Vue.config.productionTip = false

Vue.use(intercept)
Vue.use(VueRouter)
Vue.use(ElementUI)

new Vue({
  store,
  render: h => h(App),
  router: router
}).$mount('#app')

// 全局路由构造函数，判断是否登录和要跳转到页面
router.beforeEach((to, from, next) => {
  if (to.meta.requireAuth) {    // 需要登录
    if(window.sessionStorage.token && !window.sessionStorage.token==''){
      next();
    } else if (to.path !== '/') {
      let token = window.sessionStorage.token;
      if (token === 'null' || token === '' || token === undefined){
          next({path: '/'});
          Toast({ message: '检测到您还未登录,请登录后操作！', duration: 1500 })
      }
    } else {
      next();
    }
  } else {   // 不需要登录
    next();
  }
})
